package com.example.teacherservice.controller;

import com.example.teacherservice.dto.StudentCourseDTO;
import com.example.teacherservice.service.StudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student")
public class StudentCourseController {

    private final StudentCourseService studentCourseService;

    @Autowired
    public StudentCourseController(StudentCourseService studentCourseService) {
        this.studentCourseService = studentCourseService;
    }

    // 根据学生ID获取课程
    @GetMapping("/courses/{id}")
    public ResponseEntity<List<StudentCourseDTO>> getStudentCourses(@PathVariable Long id) {
        List<StudentCourseDTO> courses = studentCourseService.getCoursesByStudentId(id);
        return ResponseEntity.ok(courses);
    }

    // 新增：根据用户ID获取学生课程
    @GetMapping("/courses/by-user/{userId}")
    public ResponseEntity<List<StudentCourseDTO>> getStudentCoursesByUserId(@PathVariable Long userId) {
        List<StudentCourseDTO> courses = studentCourseService.getCoursesByUserId(userId);
        return ResponseEntity.ok(courses);
    }

    // 新增：根据用户ID获取学生ID
    @GetMapping("/id/by-user/{userId}")
    public ResponseEntity<Long> getStudentIdByUserId(@PathVariable Long userId) {
        return studentCourseService.getStudentIdByUserId(userId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    // 检查学生是否拥有某课程
    @GetMapping("/{studentId}/has-course/{courseId}")
    public ResponseEntity<Boolean> checkCourseOwnership(
            @PathVariable Long studentId,
            @PathVariable Long courseId) {
        boolean hasCourse = studentCourseService.hasCourse(studentId, courseId);
        return ResponseEntity.ok(hasCourse);
    }


}